//
//  ChannelLabel.swift
//  NetEasyNew
//
//  Created by 蔡万鸿 on 15/6/6.
//  Copyright (c) 2015年 黄花菜. All rights reserved.
//

import UIKit

protocol ChannelLabelDelegate: NSObjectProtocol {
    
    func channelLabelDidSelected(channelLabel: ChannelLabel)
}

class ChannelLabel: UILabel {
    
    var delegate: ChannelLabelDelegate?
    
    /**
    *  文字的大小比例，0最小14号字体，1最大18号字体
    */
    var scale: Double? {
        
        didSet {
            
            var max = (18.0 / 14.0) - 1.0
            var percent = (max * scale!) + 1.0
  
            // 设置 transform
            self.transform = CGAffineTransformMakeScale((CGFloat)(percent), (CGFloat)(percent))
            
            // 设置颜色 0~1
            self.textColor = UIColor(red:(CGFloat)(scale!), green: 0.0, blue: 0.0, alpha: 1.0)
        }
    }
    
    class func channelLabelWithTitle(title:String)->ChannelLabel {
        
        let l = ChannelLabel()
        l.text = title
        
        // 设置大的字体 - 为了预留出左右的空间
        l.font = UIFont.systemFontOfSize(18.0)
        l.textAlignment = NSTextAlignment.Center
        
        // 根据内容调整大小
        l.sizeToFit()
        
        // 设置小的字体
        l.font = UIFont.systemFontOfSize(14.0)
        
        // 允许用户交互
        l.userInteractionEnabled = true;
        
        return l
    }
    
    override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
        
        self.delegate?.channelLabelDidSelected(self)
    }
}
